export default {
	namespaced:true,
	state:()=>({
		cart:JSON.parse(uni.getStorageSync('cart') ||'[]')
	}),
	mutations:{
		addtocart(state,goods){
			const findResult = 	state.cart.find(x=>x.goods_id===goods.goods_id)
			if(!findResult) 
			{
				state.cart.push(goods)
			}
			else{
				findResult.goods_count++
			}
			this.commit('m_cart/saveTostroge')
		},
		// 数据的持久化存储
		saveTostroge(state){
			uni.setStorageSync('cart',JSON.stringify(state.cart))
		},
		// 更新state数据(选中和未选中)
		updatagoodsstate(state,goods){
			const findresult =  state.cart.find(x=>x.goods_id === goods.goods_id)
			if(findresult){
				findresult.goods_state = goods.goods_state
				this.commit('m_cart/saveTostroge')
			}
		},
		updatagoodscount(state,goods){
			console.log(goods)
			const findinfo = state.cart.find(x=>x.goods_id === goods.goods_id)
			if(findinfo){
				findinfo.goods_count = goods.goodS_count
				this.commit('m_cart/saveTostroge')
			}
		},
		delgoodsinfo(state,i){
			state.cart.splice(i,1)
			this.commit('m_cart/saveTostroge')
		},
		upallgoodsstate(state,flag){
			state.cart.forEach(x=>x.goods_state = flag)
			this.commit('m_cart/saveTostroge')
		}
		
	},
	getters:{
		total(state){
			// let c = 0
			// state.cart.forEach(x=>c+=x.goods_count)
			// return  c
			return  state.cart.reduce((total,item)=>total+=item.goods_count,0)
		},
		checkcount(state){
			return state.cart.filter(x=>x.goods_state).reduce((total,item)=>total+=item.goods_count,0)
		},
		allmonery(state){
			return	state.cart.filter(x=>x.goods_state).reduce((total,item)=>total+=(item.goods_price*item.goods_count),0).toFixed(2)
		}
		
	}
}